<?php
	
	function get_month($value)
	{
		$i=-1;
		$month_cache=array();
		$month_cache[]="jan";
		$month_cache[]="feb";
		$month_cache[]="mar";
		$month_cache[]="apr";
		$month_cache[]="may";
		$month_cache[]="jun";
		$month_cache[]="jul";
		$month_cache[]="aug";
		$month_cache[]="sep";
		$month_cache[]="oct";
		$month_cache[]="nov";
		$month_cache[]="dec";
		
		$value=strtolower($value);
		for ($i=0;$i<12;$i++)
			if ($month_cache[$i] == $value) return ($i+1);
		die("key {$value} not found");
	}
	
	function get_bible($value)
	{
		$bible=array();
		$bible["gen"]=1;$bible["ex"]=2;$bible["lev"]=3;
		$bible["num"]=4;$bible["deut"]=5;$bible["josh"]=6;
		$bible["jud"]=7;$bible["ruth"]=8;$bible["1sam"]=9;
		$bible["2sam"]=10;$bible["1king"]=11;$bible["2king"]=12;
		$bible["1chron"]=13;$bible["2chron"]=14;$bible["ezra"]=15;
		$bible["neh"]=16;$bible["est"]=17;$bible["job"]=18;
		$bible["ps"]=19;$bible["prov"]=20;$bible["ecc"]=21;
		$bible["solomon"]=22;$bible["is"]=23;$bible["jer"]=24;
		$bible["lam"]=25;$bible["ezek"]=26;$bible["dan"]=27;
		$bible["hos"]=28;$bible["joel"]=29;$bible["amos"]=30;
		$bible["oba"]=31;$bible["jonah"]=32;$bible["micah"]=33;
		$bible["nahum"]=34;$bible["hab"]=35;$bible["zeph"]=36;
		$bible["haggai"]=37;$bible["zech"]=38;$bible["malachi"]=39;
		$bible["matt"]=40;$bible["mark"]=41;$bible["luke"]=42;
		$bible["john"]=43;$bible["acts"]=44;$bible["rom"]=45;
		$bible["1cor"]=46;$bible["2cor"]=47;$bible["gal"]=48;
		$bible["eph"]=49;$bible["philippians"]=50;$bible["colossians"]=51;
		$bible["1thess"]=52;$bible["2thess"]=53;$bible["1timothy"]=54;
		$bible["2timothy"]=55;$bible["titus"]=56;$bible["philemon"]=57;
		$bible["heb"]=58;$bible["james"]=59;$bible["1peter"]=60;
		$bible["2peter"]=61;$bible["1john"]=62;$bible["2john"]=63;
		$bible["3john"]=64;$bible["jude"]=65;$bible["rev"]=66;
		
		$value=strtolower($value);
		if (!array_key_exists($value,$bible))
			die("ERROR: key {$value} not found");
		else
			return $bible[$value];
	}

	$file_path="/var/www/data";
	$file=fopen($file_path,"r");
	$file_out="/var/www/output";
	$fu=fopen($file_out,"w");
	$type_id=3;
	while (!feof($file))
	{
		$data=fgets($file);
		
		$first_a=strpos($data,"<a href");
		if ($first_a<1) continue;
		
		$bdate=trim(substr($data,0,$first_a-1));
		$bdate=substr($bdate,0,strlen($bdate)-1);
		// tách ngày
		$first_a=strpos($bdate," ");
		$month=trim(substr($bdate,0,$first_a));
		$day = trim(substr($bdate,$first_a));
		
		//
		$first_a=strpos($data,">");
		$data=substr($data,$first_a+1);
		$first_a=strpos($data,"</a");
		$data=substr($data,0,$first_a);
		
		// tách book và group of chapter
		$first_a=strpos($data," ");
		$book=trim(substr($data,0,$first_a));
		$data=trim(substr($data,$first_a));
		
		$first_a=strpos($data,"-");
		$from=trim(substr($data,0,$first_a));
		$to=trim(substr($data,$first_a+1));
		
		// convert book to id
		if ($from == "" || $to == "")
			die("error detected at {$month}-{$day}");
		$book=get_bible($book);
		$month = get_month($month);
		
		$out="'{$type_id}' '{$month}' '{$day}' '{$book}' '{$from}' '{$book}' '{$to}'\n";
		fputs($fu,$out);
	}
	fclose($fu);
	fclose($file);
?>
